#include "types.h"
#define _VIDEO_ROOT_
#include "video_root.h"

void scrolldown(int n)
{
	unsigned char *video, *tmp;

	for(video = (unsigned char*)DISPLAY_START;
		video < (unsigned char*)DISPLAY_END;
			video += 2) {
		tmp = (unsigned char*)(video + n * 160);

		if(tmp < (unsigned char*)DISPLAY_END)
		{
			*video = *tmp;
			*(video + 1) = *(tmp + 1);
		}
		else
		{
			*video = 0;
			*(video + 1) = 0x07;
		}
	}

	bufferY -= n;

	if(bufferY < 0)
		bufferY = 0;
}

void putcar( uchar c )
{
	unsigned char *video = (unsigned char *) ( DISPLAY_START + 2 * bufferX + 160 * bufferY );

	if(c == 10) {
		bufferX = 0;
		bufferY++;
	} else {
		*video = c;
		*(video + 1) = bufferC;

		bufferX++;

		if(bufferX == 79) {
			bufferX = 0;
			bufferY++;
		}
	}

	if(bufferY > 24)
		scrolldown(bufferY - 24);
}
